


class ListNode:
    def __init__(self,val=0,next=None):
        self.val = val
        self.next = next


def reverse(head):
    prev = None
    current_node = head
    while current_node:
        next_node = current_node.next
        current_node.next = prev
        prev = current_node
        current_node = next_node
    return prev


